package work.chenbo.starter.common.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.time.LocalDateTime;
import java.util.Random;

public class ImageCodeUtils {

    public static ImageCode getImageCode(int width,int height,int length,int timeout){
        return new ImageCode(width, height, length, timeout);
    }

    @Data
    @Slf4j
    public static class ImageCode{
        private BufferedImage image;

        private int width;

        private int height;

        private int length;

        private String code;

        private LocalDateTime expireTime;

        public ImageCode(int width,int height,int length,int timeout){
            this.width = width;
            this.height = height;
            this.length = length;
            this.expireTime = LocalDateTime.now().plusSeconds(timeout);
            StringBuilder stringBuilder = new StringBuilder();
            this.image = this.createImag(width,height,length,BufferedImage.TYPE_INT_RGB,stringBuilder);
            this.code = stringBuilder.toString();
        }

        public boolean isExpired(){
            return LocalDateTime.now().isAfter(this.expireTime);
        }

        private BufferedImage createImag(int width,int height,int length,int imageType,StringBuilder stringBuilder){
            String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑",  "楷体_GB2312" };

            Color bgColor = new Color(255, 255, 255);
            BufferedImage bufferedImage = new BufferedImage(width, height, imageType);
            Graphics2D graphics2D = (Graphics2D)bufferedImage.getGraphics();
            try{
                graphics2D.setColor(bgColor);
                graphics2D.fillRect(0,0,width,height);
                for (int i = 0; i < length; i++){
                    graphics2D.setColor(new Color(this.getRandom(0,150),this.getRandom(0,150),this.getRandom(0,150)));
                    graphics2D.setFont(new Font(fontNames[this.getRandom(0,fontNames.length)],Font.BOLD,18));
                    int theta = this.getRandom(-45,45);
                    graphics2D.rotate( theta*Math.PI/180,i*20+7,height-8);
                    graphics2D.drawString(this.getCode(stringBuilder),i*20+7,height-8);
                    graphics2D.rotate(-theta*Math.PI/180,i*20+7,height-8 );
                }
            }catch (Exception e){
                log.error("创建图形验证码异常->",e);
            }finally {
                if(graphics2D!=null){
                    graphics2D.dispose();
                }
            }
            return bufferedImage;


        }


        private int getRandom(int start,int end){
            Random random = new Random();
            return random.nextInt(end - start) + start;
        }

        private String getCode(StringBuilder stringBuilder){
            String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
            String code = String.valueOf(codes.charAt(this.getRandom(0, codes.length())));
            stringBuilder.append(code);
            return code;
        }



    }
}
